﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LicenseAutoDetection
{
    public partial class SettingsForm : Form
    {
        static readonly string driverErr = "{0} 드라이브는 추가할 수 없습니다." ;
        static readonly string existenceErr = "{0} 경로는 이미 추가하였습니다.";
        Settings settings;

        public SettingsForm(Settings s)
        {
            InitializeComponent();

            settings = s;
            chklistboxIgnorePath(settings.IgnorePath);
            chkExtension(settings.Extension);
            includeWindowsCheckBox.Checked = settings.IncludeWindows;
            includeHiddenCheckBox.Checked = settings.IncludeHidden;
            autoStartCheckBox.Checked = settings.AutoStart;
        }

        private void chkExtension(string[] ext)
        {
            jpgCheckBox.Checked = ext.Contains("jpg");
            pngCheckBox.Checked = ext.Contains("png");
            xlsCheckBox.Checked = ext.Contains("xls");
            pdfCheckBox.Checked = ext.Contains("pdf");
        }

        private string[] chkExtension()
        {
            List<string> ext = new List<string>();

            if (jpgCheckBox.Checked)
                ext.Add("jpg,jpeg");
            if (pngCheckBox.Checked)
                ext.Add("png");
            if (xlsCheckBox.Checked)
                // 확장자가 3글자일 경우 Directory.GetFiles()에서는 3글자까지만 비교하여, xlsx도 같이 검색됨
                // http://msdn.microsoft.com/en-us/library/vstudio/wz42302f(v=vs.100).aspx Note 참고
                ext.Add("xls");
            if (pdfCheckBox.Checked)
                ext.Add("pdf");

            return ext.ToArray();
        }

        private void chklistboxIgnorePath(string[] ignorePath)
        {
            ignorePathcheckedListBox.Items.AddRange(ignorePath);
        }

        private string[] chklistboxIgnorePath()
        {
            List<string> ignorePath = new List<string>();
            foreach (var item in ignorePathcheckedListBox.Items)
                ignorePath.Add(item.ToString());

            return ignorePath.ToArray();
        }

        #region Event

        private void addIgnorePathButton_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog folderDlg = new FolderBrowserDialog())
            {
                folderDlg.RootFolder = Environment.SpecialFolder.MyComputer;

                if (folderDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string[] path = folderDlg.SelectedPath.Split('\\');
                    Boolean existPath = ignorePathcheckedListBox.FindStringExact(folderDlg.SelectedPath) >= 0 ? false : true ; 
                    Boolean nullorEmpty = string.IsNullOrEmpty(path[1]) ? false : true ;
                    
                    if (existPath & nullorEmpty)
                        ignorePathcheckedListBox.Items.Add(folderDlg.SelectedPath);
                    else
                    {
                        string str = !existPath ? existenceErr : driverErr ;
                        string msg = String.Format(str, folderDlg.SelectedPath);
                        MessageBox.Show(msg, "경고", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }

        private void deleteIgnorePathButton_Click(object sender, EventArgs e)
        {
            try
            {
                while (ignorePathcheckedListBox.CheckedItems.Count > 0)
                {
                    ignorePathcheckedListBox.Items.Remove(ignorePathcheckedListBox.CheckedItems[0]);
                }
            }
            catch
            {

            }
        }

        private void btn_OkClick(object sender, EventArgs e)
        {
            settings.IgnorePath = chklistboxIgnorePath();
            settings.Extension = chkExtension();
            settings.Save();
        }

        private void includeWindowsCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            var check = sender as CheckBox;
            settings.IncludeWindows = check.Checked;
            settings.Save();
        }

        private void includeHiddenCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            var check = sender as CheckBox;
            settings.IncludeHidden = check.Checked;
            settings.Save();
        }

        private void autoStartCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            var check = sender as CheckBox;
            settings.AutoStart = check.Checked;
            settings.Save();
        }
        #endregion
    }
}
